package com.googlecode.wicketcharts;

import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.request.resource.ResourceReference;

/**
 * A registry for the JavaScript dependencies used by wicket-charts. By default,
 * all JavaScript references will be loaded from the web. If you want to package
 * your own JavaScript files or change the URLs, you can do so by calling the
 * setter methods from the init() method of your wicket application.
 * 
 * @author Tom Hombergs (tom.hombergs@gmail.com)
 * 
 */
public class JavaScriptResourceRegistry {

	public static final String DEFAULT_JQUERY_URL = "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js";

	public static final String DEFAULT_HIGHCHARTS_URL = "http://code.highcharts.com/highcharts.js";

	public static final String DEFAULT_HIGHCHARTS_EXPORTING_URL = "http://code.highcharts.com/modules/exporting.js";

	private RegistryEntry jqueryEntry = new RegistryEntry(DEFAULT_JQUERY_URL);

	private RegistryEntry highchartsEntry = new RegistryEntry(
			DEFAULT_HIGHCHARTS_URL);

	private RegistryEntry highchartsExportingEntry = new RegistryEntry(
			DEFAULT_HIGHCHARTS_EXPORTING_URL);

	private static JavaScriptResourceRegistry INSTANCE;

	private JavaScriptResourceRegistry() {

	}

	public static JavaScriptResourceRegistry getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new JavaScriptResourceRegistry();
		}
		return INSTANCE;
	}

	public void setJQueryReference(String url) {
		this.jqueryEntry = new RegistryEntry(url);
	}

	public void setJQueryReference(ResourceReference reference) {
		this.jqueryEntry = new RegistryEntry(reference);
	}

	public void setHighchartsReference(String url) {
		this.highchartsEntry = new RegistryEntry(url);
	}

	public void setHighchartsReference(ResourceReference reference) {
		this.highchartsEntry = new RegistryEntry(reference);
	}

	public void setHighchartsExportingReference(String url) {
		this.highchartsExportingEntry = new RegistryEntry(url);
	}

	public void setHighchartsExportingReference(ResourceReference reference) {
		this.highchartsExportingEntry = new RegistryEntry(reference);
	}

	public RegistryEntry getJQueryEntry() {
		return jqueryEntry;
	}

	public RegistryEntry getHighchartsEntry() {
		return highchartsEntry;
	}

	public RegistryEntry getHighchartsExportingEntry() {
		return highchartsExportingEntry;
	}

	public class RegistryEntry {

		private String url;

		private ResourceReference reference;

		public RegistryEntry(String url) {
			this.url = url;
		}

		public RegistryEntry(ResourceReference reference) {
			this.reference = reference;
		}

		public void addToHeaderResponse(IHeaderResponse response) {
			if (this.url != null) {
				response.renderJavaScriptReference(url);
			} else if (this.reference != null) {
				response.renderJavaScriptReference(reference);
			} else {
				throw new IllegalStateException(
						"A RegistryEntry must have at least a non-null url or a non-null reference!");
			}
		}

	}

}
